﻿using System;
using System.Reflection;
using System.Collections.Generic;
using NLog;
using dlCentral.Utils;
using dlCentral.PluginHandlers;
using System.Xml;
using System.Net;
using System.Text.RegularExpressions;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;

namespace dlCentral.TorrentHandlers {
    internal class ATHandler : TorrentHandler {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        string size = "";
        string seeds = "";
        string peers = "";
        

        // The torrent ID for the plugin this handler implements support for.
        public override TorrentId ID
        {
            get { return TorrentId.AT; }
            set { }
        }

        // Should return the name of the source Torrent providing the data.
        public override string TorrentName {
            get { return "AliveTorrents"; }
            set { }
        }

        private void GetTorrentURL(string torurl, MediaDetail item)
        {

            try
            {
  //          http://www.AliveTorrents.com/torrent/8961330/swamp-loggers-s03e04-720p-hdtv-x264-momentum

  //          http://www.alivetorrents.com/dl/8961330
                torurl = torurl.Replace("torrent", "dl");
                torurl = torurl.Substring(0,torurl.LastIndexOf("/"));
                item.TorrentURL = torurl;
            }
            catch { return; }
        }

        public override void GetDetails(List<MediaDetail> Results)
        {
            if (DLSType == SiteSearchType.TVSHOW) url = string.Format("http://www.alivetorrents.com/rss/search/{0}/date", SearchTerm);
            if (DLSType == SiteSearchType.IMDb) url = "http://www.alivetorrents.com/rss/cat/4";

            JObject jobject = new JObject();
            string date = "";
            XmlNodeList xml = getXML(url);
            if (xml != null)
            {
            XmlNodeList Nodes = xml.Item(0).SelectNodes("item");
            if (Nodes != null)
                foreach (XmlNode currNode in Nodes)
                {
                    MediaDetail item = new MediaDetail();

                    foreach (XmlNode node in currNode.ChildNodes)
                    {
                        string value = node.InnerText;
                        switch (node.Name)
                        {
                            case "title":
                                item.Name = value;
                                break;
                            case "category":
                                Category = value;
                                break;
                            case "size":
                                size = value;
                                break;
                            case "dc:date":
                                date = value;
                                break;
                            case "seeds":
                                seeds = Convert.ToString(value);
                                break;
                            case "leechers":
                                peers = Convert.ToString(value);
                                break;
                            case "link":
                                GetTorrentURL(value, item);
                                break;
                        }
                    }


                    item.Date = DateTime.Parse(date);
                    item.SearchType = DLSType;
                    item.Site = Site;
                    size = dlCentralUtils.FormatByteSize(Convert.ToInt64(size));
                    item.TorrentInfo = size + " S/P : " + seeds + "/" + peers;
                    if (DLSType == SiteSearchType.TVSHOW) item.IsInfoChecked = true;
                    if (item.TorrentURL != null && item.TorrentURL != "") Results.Add(item);
                }
            }



        }

        protected override bool IsAvailable()
        {
            return true;
        }


    }
}
